﻿#include "systemstatewid.h"
#include <QTimer>
#include <qmessagebox.h>

#include "componentmanager.h"

SystemStateWid::SystemStateWid(QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	ui.label->setMinimumHeight(50);
	m_state = component->GetComponent(Component::component_systemInfo);
	if (m_state == nullptr) {
		ui.label->setText("系统状态无法获取");
		ui.label->setStyleSheet("font: 75 26pt \"隶书\"; background-color: rgb(255, 0, 0);");
		//ui.pushButton->setDisable(true);
		//ui.pushButton_2->setDisable(true);
		//ui.pushButton_3->setDisable(true);
	} else {
		CheckSystemState();
	}
}

SystemStateWid::~SystemStateWid()
{
}

void SystemStateWid::CheckSystemState()
{
	QByteArray output;
	bool ret = m_state->ExcuteCmd((int)SystemCmdType::system_cmd_get_state, {}, output);
	if (!ret) {
		ui.label->setText("系统状态无法获取");
		ui.label->setStyleSheet("font: 75 26pt \"隶书\"; background-color: rgb(255, 0, 0);");
	} else {
		int state = QString(output).toInt();
		if (state == 1) {
			ui.label->setToolTip("按下启动按钮，系统切换到正常状态");
			ui.label->setText("系统状态: 等待启动");
			ui.label->setStyleSheet("font: 75 26pt \"隶书\"; background-color: rgb(125, 125, 125);");
		} else if (state == 2) {
			ui.label->setToolTip("正常状态");
			ui.label->setText("系统状态: 正常状态");
			ui.label->setStyleSheet("font: 75 26pt \"隶书\"; background-color: rgb(0, 255, 0);");
		} else if (state == 3) {
			ui.label->setToolTip("系统停止，问题处理完成后，点击复位按钮恢复");
			ui.label->setText("系统状态: 停止状态");
			ui.label->setStyleSheet("font: 75 26pt \"隶书\"; background-color: rgb(255, 0, 0);");
		} else {
			ui.label->setToolTip("联系开发人员，或重启软件恢复");
			ui.label->setText("系统BUG");
			ui.label->setStyleSheet("font: 75 26pt \"隶书\"; background-color: rgb(255, 0, 0);");
		}
	}

	QTimer::singleShot(1000, this, &SystemStateWid::CheckSystemState);
}

void SystemStateWid::on_pushButton_clicked()
{
	QByteArray output;
	m_state->ExcuteCmd((int)SystemCmdType::system_cmd_system_stop, {}, output);
}
void SystemStateWid::on_pushButton_2_clicked()
{
	QByteArray output;
	m_state->ExcuteCmd((int)SystemCmdType::system_cmd_System_reset, {}, output);
}
void SystemStateWid::on_pushButton_3_clicked()
{
	QByteArray output;
	m_state->ExcuteCmd((int)SystemCmdType::system_cmd_System_start, {}, output);
}
